package com.audience;

import jdk.nio.Channels;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.Set;

import static java.nio.file.StandardOpenOption.*;


public class App {

    public static void main(String[] args) {
/*        try {
            URL url=App.class.getClassLoader().getResource("aa.txt");
            System.out.println(url);
            URI uri = App.class.getClassLoader().getResource("aa.txt").toURI();
            Path path = Path.of(uri);
            System.out.println(path.getFileName());
            long size = Files.size(path);
            System.out.println(size);
            String a="12324";
            Files.writeString(path,a,TRUNCATE_EXISTING);
            System.out.println(Files.readString(path));
            System.out.println(path);
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }*/

        try {
            URI uri = App.class.getClassLoader().getResource("aa.txt").toURI();
            Path path = Path.of(uri);

            FileChannel fileChannel = FileChannel.open(path,TRUNCATE_EXISTING);
            ByteBuffer byteBuffer= ByteBuffer.allocate(2);
//            System.out.println(fileChannel.size());
//            fileChannel.position(0);
//            System.out.println(fileChannel.position());
            while (fileChannel.read(byteBuffer)!=-1){
                Thread.sleep(1000);
                System.out.println("======"+fileChannel.read(byteBuffer)+"===="+byteBuffer.position());
                byteBuffer.flip();
                while (byteBuffer.hasRemaining()){
                    System.out.println((char) byteBuffer.get());
                }
                Charset charset = Charset.forName("utf-8");
                CharBuffer charBuffer = charset.decode(byteBuffer);
                String s = charBuffer.toString();
                System.out.println(s);
                byteBuffer.clear();
            }
            fileChannel.close();


//            byteBuffer.flip();
//            fileChannel.write(byteBuffer);

            /*Selector selector = Selector.open();
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(false);
            SelectionKey selectionKey=serverSocketChannel.register(selector,SelectionKey.OP_READ);*/
        } catch (IOException | URISyntaxException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void selector(){
        ByteBuffer buffer=ByteBuffer.allocate(1024);
        try {
            Selector selector=Selector.open();
            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ssc.socket().bind(new InetSocketAddress(8080));
            ssc.register(selector,SelectionKey.OP_ACCEPT);
            while (true){
                Set selectedKeys = selector.selectedKeys();
                Iterator it = selectedKeys.iterator();
                while (it.hasNext()){
                    SelectionKey key= (SelectionKey) it.next();
                    if (key.isAcceptable()){
                        ServerSocketChannel ssChannel = (ServerSocketChannel) key.channel();
                        SocketChannel sc = ssChannel.accept();
                        sc.configureBlocking(false);
                        sc.register(selector,SelectionKey.OP_READ);
                        it.remove();
                    } else if (key.isReadable()){
                        SocketChannel sc = (SocketChannel) key.channel();
                        while (true){
                            buffer.clear();
                            int n =  sc.read(buffer);
                            if (n<=0){
                                break;
                            }
                            buffer.flip();
                        }
                        it.remove();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
